var _mrpId, _moId, _productId;

var thisPage = {
    // 初始化页面
    OnPageLoad: function () {
        $.ajax({
            type: "POST",
            url: parent.basePath + "/trade/production_instruction/lines/fetchProductColourByMoHeaderKey.html",
            timeout: 60 * 1000,
            data: JSON.stringify(_moId),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            thisPage.SetProductColourItemList(data);
        }).fail(function (data) {
            $('#loading').hide();
            ajaxError(data, 1);
            $(".error-msg").css({top: '38px'});
        }).always(function () {
        });
    },
    // 设置颜色选择下拉列表
    SetProductColourItemList: function (data) {
        $("#ProductColourItemList").empty();
        var options = [];
        options.push('<option value="0">-- 选择成品色 --</option>');
        for (var i = 0; i < data.length; i++) {
            options.push('<option value="');
            options.push(data[i].colourid);
            options.push('">');
            options.push(data[i].colourname);
            options.push('</option>');
        }
        $("#ProductColourItemList").html(options.join(''));
        // 根据默认的第一个成品色提取打印预览的数据
        //thisPage.fetchPrintPreviewData();
        $('#loading').hide();
    },
    // 得到打印预览列表数据
    fetchPrintPreviewData: function () {
        if (parseInt($("#ProductColourItemList").val()) === 0) {
            return false;
        }
        $("#ProductColourName").text($("#ProductColourItemList option:selected").text());
        var params = {};
        params['HeaderId'] = parseInt(_mrpId);
        params['MoHeaderId'] = parseInt(_moId);
        params['ProductColourId'] = parseInt($("#ProductColourItemList").val());
        params['ProductId'] = parseInt(_productId);
        params['CurrentUserId'] = null;
        params['WorkFlowId'] = null;

        $.ajax({
            type: "POST",
            url: window.basePath + "/tech/mrp/fetchPrintPreviewData.html",
            timeout: 30 * 1000,
            data: JSON.stringify(params),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            let item = data.HeaderEntity;
            $("#CustomerName").text(item.customerName);
            $("#ContractNo").text(item.contractNo);
            $("#PreparedByName").text(item.preparedByName);
            $("#GradedByName").text(item.gradedByName);
            $("#PreparedDate").text(item.preparedDate);
            $("#StyleNo").text(item.styleNo);
            $("#ProductName").text(item.productName);
            $("#Description").text(item.description);
            // 每色数量
            let itemQuantity = data.ProductQuantity;
            $("#Quantity").text(itemQuantity.quantity);
            // 显示明细数据
            linesHandler.BuildDataTable(data.MrpLinesList);
        }).fail(function (data) {
            $('#loading').hide();
            ajaxError(data, 1);
            $(".error-msg").css({top: '38px'});
        }).always(function () {
        });
    },
    // html打印
    DoPreview: function () {
        window.print();
    }
};

var linesHandler = {
    BuildDataTable: function (result) {
        $("#table_report tbody").empty();
        if (result === null) {
            return false;
        }
        var content_01 = '';
        var seq = 0;
        for (var i = 0; i < result.length; i++) {
            seq = seq + 1;
            content_01 += '<tr>';
            content_01 += '<td class="cell">' + seq + '</td>';
            content_01 += '<td class="cell">' + formatNullData(result[i].destination) + '</td>';
            content_01 += '<td class="cell">' + formatNullData(result[i].materialName) + '</td>';
            content_01 += '<td class="cell">' + formatNullData(result[i].materialCode) + '</td>';
            content_01 += '<td class="cell">' + formatNullData(result[i].partOfNeed) + '</td>';
            if (parseInt($("#Dept").val()) === 2) {
                content_01 += '<td class="cell">' + result[i].supplierName + '</td>';
            } else {
                content_01 += '<td class="cell">&nbsp;</td>';
            }
            content_01 += '<td class="cell">' + formatNullData(result[i].materialColourName) + '</td>';  // 材料色
            content_01 += '<td class="cell">' + formatNullData(result[i].productSizeName) + '</td>';     // 成品规格（尺码）
            content_01 += '<td class="cell">' + formatNullData(result[i].specification) + '</td>';       // 规格即毛幅
            content_01 += '<td class="cell">' + formatNullData(result[i].uomName) + '</td>';             // 单位
            content_01 += '<td class="cell num">' + formatNullData(result[i].factoryPieceYardage === 0 ? null : result[i].factoryPieceYardage) + '</td>';     // 工厂用量
            //content_01 += '<td class="cell num">' + formatNullData(result[i].s_PieceYardage === 0 ? null : result[i].s_PieceYardage) + '</td>';     // 套裁量
            content_01 += '<td class="cell num">' + result[i].planDemandedQuantity + '</td>';                    // 计划需求量
            content_01 += '<td class="cell num">' + formatNullData(result[i].priorDemandedQuantity) + '</td>';   // 提前组料数
            if (parseInt($("#Dept").val()) === 2) {
                content_01 += '<td class="cell num">' + formatNullData(result[i].stockLevels) + '</td>';         // 库存数
                content_01 += '<td class="cell num">' + result[i].demandedQuantity + '</td>';                    // 补给数
            } else {
                content_01 += '<td class="cell num">&nbsp;</td>';             // 库存数
                content_01 += '<td class="cell num">&nbsp;</td>';             // 补给数
            }
            content_01 += '<td class="cell normal">' + formatNullData(result[i].description) + '</td>';          // 备注
            //content_01 += '<td class="cell">' + formatNullData(result[i].productSizeName) + '</td>';     // 成品码            
            //content_01 += '<td class="cell normal">' + formatNullData(result[i].fabricWidth) + '</td>';         // 净幅
            //content_01 += '<td class="cell normal">' + formatNullData(result[i].productColourName) + '</td>';   // 成品色
            content_01 += '</tr>';

            $("#table_report tbody").append(content_01);
            content_01 = '';
        }
    }
};

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    // 从url得到相关参数
    _mrpId = jQuery.url.param("id");
    _moId = jQuery.url.param("moId");
    _productId = jQuery.url.param("productId");
    $("#printed_date").text(getCurrentDate());    // 打印日期

    thisPage.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}